package tk.capkiller.sharelinks.biz;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

public class CaptchaBreaker {
	public final static String PATH_TESTIMAGE = "D:\\patrick\\workspace\\house1909\\capkiller\\resources";
	public final static boolean DEBUG_MODE = false;

	public static void main(String[] args) {
		System.out.println("try to break " + args[0]);
		CaptchaBreaker breaker = new CaptchaBreaker();
		breaker.breakeIt(args[0]);
		System.out.println("finish");

	}

	private BufferedImage loadImage(String filename) throws IOException {
		File file = new File(filename);
		FileInputStream fis = new FileInputStream(file);
		BufferedImage image = ImageIO.read(fis);
		return image;
	}

	public Point breakeIt(String path) {
		File input = new File(path);

		BufferedImage inputImage = null;
		Point gridSize = new Point(14, 15);
		ValuesInAGrid gridValues;
		try {
			inputImage = loadImage(path);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

		// ########## Dimension of Image
		GridCounter rowCounter = new GridCounter(inputImage);
		gridValues = rowCounter.countGrid();
		gridSize = gridValues.getSize();
		System.out.println("GridSize: " + gridSize.x + "x" + gridSize.y);

		// ########## Value of Center Image
		StringIntegerPair valCenter = EasyImage.getValuesOfCenterImage(
				inputImage, input);

		CompareImage resultSmallValue = gridValues.search(valCenter);
		BufferedImage mark = new BufferedImage(inputImage.getWidth(),
				inputImage.getHeight(), inputImage.getType());
		mark.createGraphics().drawImage(inputImage, 0, 0, null);

		mark.createGraphics().drawRect(
				resultSmallValue.getTopLeftCut().x,
				resultSmallValue.getTopLeftCut().y,
				resultSmallValue.getButtomRightCut().x
						- resultSmallValue.getTopLeftCut().x,
				resultSmallValue.getButtomRightCut().y
						- resultSmallValue.getTopLeftCut().y);
		mark.createGraphics().drawLine(resultSmallValue.getTopLeftCut().x,
				resultSmallValue.getTopLeftCut().y,
				resultSmallValue.getButtomRightCut().x,
				resultSmallValue.getButtomRightCut().y);
		mark.createGraphics().drawLine(resultSmallValue.getButtomRightCut().x,
				resultSmallValue.getTopLeftCut().y,
				resultSmallValue.getTopLeftCut().x,
				resultSmallValue.getButtomRightCut().y);
		testWriteImage(mark, path + "mark.gif");

		// gridValues.saveAllImages("C:\\temp\\smallimages\\");
		// testCREATEsmallCOMPAREiMAGES(input, gridValues);

		return gridSize;
	}

	// public void testCREATEsmallCOMPAREiMAGES(File input, ValuesInAGrid
	// images) {
	// {
	// Iterator<CompareImage> itrImages = images.iterator();
	//
	// while (itrImages.hasNext()) {
	// CompareImage compImage = itrImages.next();
	// StringIntegerPair val = EasyImage.getValuesOfSmallImage(
	// compImage.getImage(), input);
	// }
	// }
	// }

	public static void testWriteImage(BufferedImage image, String fileName) {
		if (DEBUG_MODE) {
			try {
				ImageIO.write(image, "gif", new File(fileName));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
